package com.dkd.manage.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.dkd.common.utils.DateUtils;
import com.dkd.common.utils.uuid.UUIDUtils;
import com.dkd.manage.domain.Channel;
import com.dkd.manage.domain.Node;
import com.dkd.manage.domain.VmType;
import com.dkd.manage.mapper.ChannelMapper;
import com.dkd.manage.mapper.NodeMapper;
import com.dkd.manage.mapper.VmTypeMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dkd.manage.mapper.VendingMachineMapper;
import com.dkd.manage.domain.VendingMachine;
import com.dkd.manage.service.IVendingMachineService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 设备管理Service业务层处理
 * 
 * @author muchun
 * @date 2025-07-25
 */
@Service
public class VendingMachineServiceImpl implements IVendingMachineService 
{
    @Autowired
    private VendingMachineMapper vendingMachineMapper;
    @Autowired
    VmTypeMapper vmTypeMapper;
    @Autowired
    NodeMapper nodeMapper;
    @Autowired
    ChannelMapper channelMapper;

    /**
     * 查询设备管理
     * 
     * @param id 设备管理主键
     * @return 设备管理
     */
    @Override
    public VendingMachine selectVendingMachineById(Long id)
    {
        return vendingMachineMapper.selectVendingMachineById(id);
    }

    /**
     * 查询设备管理列表
     * 
     * @param vendingMachine 设备管理
     * @return 设备管理
     */
    @Override
    public List<VendingMachine> selectVendingMachineList(VendingMachine vendingMachine)
    {
        return vendingMachineMapper.selectVendingMachineList(vendingMachine);
    }

    /**
     * 新增设备管理
     * 
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertVendingMachine(VendingMachine vendingMachine)
    {
        //1新增设备
        //1-1生成设备编号：UUIDUtils.getUUID()
        String uuid = UUIDUtils.getUUID();
        vendingMachine.setInnerCode(uuid);
        //1-2根据设备类型对象，获取售货机容量
        VmType vmType = vmTypeMapper.selectVmTypeById(vendingMachine.getVmTypeId());
        vendingMachine.setChannelMaxCapacity(vmType.getChannelMaxCapacity());
        //2根据点位id获取点位信息
        Node node = nodeMapper.selectNodeById(vendingMachine.getNodeId());
        //利用beanutils，将点位信息复制到设备对象中
        BeanUtils.copyProperties( node,vendingMachine,"id");
        vendingMachine.setAddr(node.getDetailedAddress());
        //商圈类型更新
        vendingMachine.setBusinessType(node.getBusinessDistrictType());
        //3设置创建时间和更新时间
        vendingMachine.setCreateTime(DateUtils.getNowDate());
        vendingMachine.setUpdateTime(DateUtils.getNowDate());
        int result = vendingMachineMapper.insertVendingMachine(vendingMachine);
        //计算货道数量
        int row = vmType.getVmRow().intValue();
        int col = vmType.getVmCol().intValue();
        List<Channel> channelList = new ArrayList<Channel>();
        //生成货道信息
        for(int i=1;i<=row;i++){
            for(int j=1;j<=col;j++){
                Channel channel = new Channel();
                channel.setChannelCode(i+"-"+j);
                channel.setVmId(vendingMachine.getId());
                channel.setInnerCode(uuid);
                channel.setMaxCapacity(vmType.getChannelMaxCapacity());
                channel.setCurrentCapacity(0L);
                channel.setLastSupplyTime(DateUtils.getNowDate());
                channel.setCreateTime(DateUtils.getNowDate());
                channel.setUpdateTime(DateUtils.getNowDate());
//                使用批量插入，fancy，效率高
//                channelMapper.insertChannel(channel);
                channelList.add(channel);
            }
        }
        channelMapper.insertChannelBatch(channelList);
        return result;
    }

    /**
     * 修改设备管理
     * 
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Override
    public int updateVendingMachine(VendingMachine vendingMachine)
    {
        //1-2根据设备类型对象，获取售货机容量
        VmType vmType = vmTypeMapper.selectVmTypeById(vendingMachine.getVmTypeId());
        vendingMachine.setChannelMaxCapacity(vmType.getChannelMaxCapacity());
        //2根据点位id获取点位信息
        Node node = nodeMapper.selectNodeById(vendingMachine.getNodeId());
        //利用beanutils，将点位信息复制到设备对象中
        BeanUtils.copyProperties( node,vendingMachine,"id");
        vendingMachine.setAddr(node.getDetailedAddress());
        //商圈类型更新
        vendingMachine.setBusinessType(node.getBusinessDistrictType());
        //3设置更新时间
        vendingMachine.setUpdateTime(DateUtils.getNowDate());

        vendingMachine.setUpdateTime(DateUtils.getNowDate());
        return vendingMachineMapper.updateVendingMachine(vendingMachine);
    }

    /**
     * 批量删除设备管理
     * 
     * @param ids 需要删除的设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineByIds(Long[] ids)
    {
        return vendingMachineMapper.deleteVendingMachineByIds(ids);
    }

    /**
     * 删除设备管理信息
     * 
     * @param id 设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineById(Long id)
    {
        return vendingMachineMapper.deleteVendingMachineById(id);
    }

    /**
     * 根据内部编码查询设备管理
     *
     * @param innerCode 内部编码
     * @return 设备管理
     */
    @Override
    public VendingMachine selectVendingMachineByInnerCode(String innerCode) {
        return vendingMachineMapper.selectVendingMachineByInnerCode(innerCode);
    }
}
